package com.etrans.web.gps.business.driverTask.service;
/**
 *司机报警短信通知
 *
 */
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.driverTask.domain.DriverAlarmVO;
import com.etrans.web.gps.business.platformTask.timer.SendPlatMessage;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.common.SendMessageRecord;
import com.etrans.web.gps.util.message.SMSUtil;
@Service
public class SendShortMsgService extends BaseService{
	private static final Logger logger = LoggerFactory.getLogger(SendShortMsgService.class);
	@SuppressWarnings("null")
	public SendMessageRecord queryDriverAnalyzes() throws Exception{
		//前一天
		SimpleDateFormat sd = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		Date todayDate = new Date();
		Calendar cdate = Calendar.getInstance();
		cdate.setTime(todayDate);
		cdate.set(Calendar.DATE, cdate.get(Calendar.DATE) - 1);
		String sendDate = sdf.format(cdate.getTime());
		List<DriverAlarmVO> barList =new ArrayList<DriverAlarmVO>();
		HashMap<String, Object> param =new HashMap<String, Object>();
		HashMap<String, String> driverMap =new HashMap<String, String>();
		HashMap<String, Object> whereMap =new HashMap<String, Object>();
		SendMessageRecord smr = new SendMessageRecord();
		List<HashMap<String, Object>> driverList = selectList("alarmTypeQuery.queryDriverAlarmInfo",param);
			if(null!=driverList||driverList.size()!=0){
				for (HashMap<String, Object> driverInfoMap : driverList) {
					String driverName = String.valueOf(driverInfoMap.get("name"));
					String driverPhone = String.valueOf(driverInfoMap.get("phone"));
					driverMap.put(driverName, driverPhone);
					}
				}
				barList = MGTemplete.find("egov", "driver_alarm_swap", whereMap,DriverAlarmVO.class,true);
				if(barList!=null){
					for  (DriverAlarmVO driver: barList) {
						String driName =null;
						String driverPhone=null;
						StringBuffer driverbuff = new StringBuffer();
						
				if (driver.getAnalyse_date()!=null) {
					String analyseDate = sdf.format(driver.getAnalyse_date());
					if(null!=driver.getDriverName()&&driverMap.containsKey(driver.getDriverName())&&sendDate.equals(analyseDate)){
						driName= driver.getDriverName();
						driverPhone = driverMap.get(driName);
						int overspeedCount = driver.getOverspeed_count();//超速报警次数
						int over_loadCount = driver.getOver_load_count();//超员报警次数
						int nightly_3rdCount = driver.getNightly_3rd_count();//夜间三级禁行报警次数
						int prohibit_drivingCount = driver.getProhibit_driving_count();//2-5点禁行报警次数
						int roadoffsetCount = driver.getRoadoffset_count();//偏离路线报警次数
						int tiredCount = driver.getTired_count();//疲劳驾驶次数
					if (overspeedCount==0 && over_loadCount==0 && nightly_3rdCount==0
						&& prohibit_drivingCount==0 && roadoffsetCount==0 && tiredCount==0) {
						driverbuff=null;
							}
					try {
						if (driverbuff!=null) {
							driverbuff.append(driName+"驾驶员您好，您于"+ analyseDate+"发生：");
							if(overspeedCount!=0){
								driverbuff.append("超速报警"+overspeedCount+"次，");
								}
							if(over_loadCount!=0){
								driverbuff.append("超员报警"+over_loadCount+"次，");
								}
							if(nightly_3rdCount!=0){
								driverbuff.append("夜间三级禁行报警"+nightly_3rdCount+"次，");
								}
							if(prohibit_drivingCount!=0){
								driverbuff.append("2-5点禁行报警"+prohibit_drivingCount+"次，");
								}
							if(roadoffsetCount!=0){
								driverbuff.append("偏离路线报警"+roadoffsetCount+"次，");
								}
							if(tiredCount!=0){
										driverbuff.append("疲劳驾驶报警"+tiredCount+"次，");
								}
							driverbuff.append("请注意！");
							SMSUtil.mt(driverPhone, driverbuff.toString(), "", "","");
							smr.set_id(new ObjectId());
							smr.setPhone(driverPhone);
							smr.setTips_time(new Date());
							smr.setTips_type(0);//0-短信  1-邮件
							smr.setTarget_type(2);//0-平台联系人  1-业户联系人 2-从业人员   3-区域联系人  4-车辆联系人
							smr.setTarget_name(driName);
							smr.setContent(driverbuff.toString());
							MGTemplete.insertOne("egov", "tips_record", smr);
								}
							} catch (Exception e) {
								logger.error("短信发送失败！",e);
								e.printStackTrace();
							}
						}
					}
				}
			}
		return smr;	
	}
}